package com.iteaj.demo.client.controller;

import com.iteaj.demo.client.protocol.TradeTypeDemo;
import com.iteaj.iot.client.json.AppClientProtocol;
import com.iteaj.network.Message;
import com.iteaj.network.client.ClientMessage;
import com.iteaj.network.client.app.AppClientMessage;
import com.iteaj.network.client.app.AppClientMessageBody;
import com.iteaj.network.client.app.AppJsonMessageBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * create time: 2021/5/29
 * @see TradeTypeDemo
 * 服务端接收对象{@code ClientRequestDemoCtrl}
 * @author iteaj
 * @since 1.0
 */
@Controller
@RequestMapping("iot/demo")
public class ClientServerController {

    /**
     * 请求设备管理平台
     *
     * 设备管理平台 -> 响应客户端
     */
    @GetMapping("req")
    public void request() {
        String deviceSn = "010203"; // 要操作的设备的设备编号
        AppJsonMessageBody messageBody = AppClientMessageBody.jsonMessageBody("content", "58");

        // 创建要请求的报文
        AppClientMessage message = AppClientMessage
                .getInstance(deviceSn, TradeTypeDemo.client_request_demo, messageBody);

        // 构建请求协议, 请求服务端
        AppClientProtocol.buildRequestProtocol(message).request(protocol -> {
            final ClientMessage clientMessage = protocol.responseMessage();
            final Message.MessageBody body = clientMessage.getBody();
            return null;
        });
    }

    /**
     * 请求设备管理平台
     *
     * 设备管理平台 -> 设备 -> 设备响应平台 -> 平台响应客户端
     */
    @GetMapping("waiting")
    public void waiting() {
        String deviceSn = "010203"; // 要操作的设备的设备编号
        AppJsonMessageBody messageBody = AppClientMessageBody.jsonMessageBody("content", "58");

        // 创建要请求的报文, 并设置等待设备返回
        AppClientMessage message = AppClientMessage
                .getInstanceOfWaiting(deviceSn, TradeTypeDemo.client_request_demo, messageBody);

        // 构建请求协议, 请求服务端
        AppClientProtocol.buildRequestProtocol(message).request(protocol -> {
            final ClientMessage clientMessage = protocol.responseMessage();
            final Message.MessageBody body = clientMessage.getBody();
            return null;
        });
    }
}
